import { Equal } from "@type-challenges/utils";

// 方法1
// export type LastIndexOf<
//   T,
//   U,
//   isAbort extends boolean = false,
//   Result extends any[] = []
// > = T extends [...infer Rest, infer L]
//   ? Equal<L, U> extends true
//     ? isAbort extends false
//       ? LastIndexOf<Rest, U, true, []>
//       : LastIndexOf<Rest, U, true, [...Result, any]>
//     : isAbort extends false
//     ? LastIndexOf<Rest, U, false, []>
//     : LastIndexOf<Rest, U, true, [...Result, any]>
//   : isAbort extends false
//   ? -1
//   : Result["length"];
// 方法2

export type LastIndexOf<T, U> = T extends [...infer Rest, infer L]
  ? Equal<L, U> extends true
    ? Rest["length"]
    : LastIndexOf<Rest, U>
  : -1;

type Result555 = LastIndexOf<[1, 2, 3, 2, 1], 2>;
